﻿<%@ WebHandler Language="C#" Class="proxy" %>

using System;
using System.Web;
using System.Net;

public class proxy : IHttpHandler {

    string host = "http://139.224.131.42:58001";
    string contentType = "application/json";

    public void ProcessRequest (HttpContext context) {
        HttpRequest req = context.Request;
        string api = req.QueryString["api"];
        string res = string.Empty;
        if(api.IndexOf("http")!=0){
            api = host + api;
        }

        WebClient wc = new WebClient();
        wc.Encoding = System.Text.Encoding.UTF8;
        wc.Headers.Add("Accept", contentType);
        wc.Headers.Add("ContentType", contentType);
        
        if (req.HttpMethod == "POST") {
            var sr = new System.IO.StreamReader(req.InputStream, System.Text.Encoding.UTF8);
            string postdata = sr.ReadToEnd();
            sr.Close();
            res = wc.UploadString(api, "post", postdata);
        } else {
            string query = "?_=";
            foreach (string k in req.QueryString.AllKeys) {
                query += string.Format("&{0}={1}", k, req.QueryString[k]);
            }
            res = wc.DownloadString(api + query);
        }
        wc.Dispose();

        context.Response.ContentType = contentType;
        context.Response.Write(res);
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}